﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace MC34063_Universal_Calculator
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        enum ViewerState { Calculation, Schematic };

        ParametersPage _paramsPage = null;
        ViewerState _viewerState = ViewerState.Calculation;

        public MainWindow()
        {
            InitializeComponent();
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
        }

        void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        {
            if (e.Category == UserPreferenceCategory.Locale)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CurrentUICulture;
                WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = System.Globalization.CultureInfo.CurrentUICulture;
            }
        }

        private void MenuItem_Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CommandBinding_New_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            _paramsPage.Clear();
        }

        private void MenuItem_Help_Info_Click(object sender, RoutedEventArgs e)
        {
            MC34063_Universal_Calculator_AboutBox aboutBox = new MC34063_Universal_Calculator_AboutBox(this);
            aboutBox.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _paramsPage = new ParametersPage();
            _paramsPage.SchematicClick += new RoutedEventHandler(MenuItem_Ansicht_Schaltung_Click);
            Menu_Options_UseDefaultParts.IsChecked = _paramsPage.UseDefaultParts;
            mainFrame.Navigate(_paramsPage);
            this.Visibility = System.Windows.Visibility.Visible;
        }

        private void MenuItem_Ansicht_Berechnung_Click(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(_paramsPage);
            MenuItemAnsichtSchaltung.IsChecked = false;
            _paramsPage.UpdateSchematicFrame();
            _viewerState = ViewerState.Calculation;
        }

        private void MenuItem_UseDefaultParts_Click(object sender, RoutedEventArgs e)
        {
            _paramsPage.UseDefaultParts = Menu_Options_UseDefaultParts.IsChecked;
        }

        private void MenuItem_Ansicht_Schaltung_Click(object sender, RoutedEventArgs e)
        {
            if (_paramsPage.SchematicPage != null)
            {
                Size prevSize = new Size(this.ActualWidth, this.ActualHeight);
                switch (_paramsPage.CurrentRegType)
                {
                    case OutputData.RegulatorType.UP:
                        try
                        {
                            ((MC34063A_StepUp_Page)_paramsPage.SchematicPage).Click -= new MouseButtonEventHandler(SchematicClick);
                        }
                        catch (Exception) { }
                        ((MC34063A_StepUp_Page)_paramsPage.SchematicPage).Click += new MouseButtonEventHandler(SchematicClick);
                        mainFrame.Navigate(_paramsPage.SchematicPage);
                        break;
                    case OutputData.RegulatorType.DOWN:
                        try
                        {
                            ((MC34063A_StepDown_Page)_paramsPage.SchematicPage).Click -= new MouseButtonEventHandler(SchematicClick);
                        }
                        catch (Exception) { }
                        ((MC34063A_StepDown_Page)_paramsPage.SchematicPage).Click += new MouseButtonEventHandler(SchematicClick);
                        mainFrame.Navigate(_paramsPage.SchematicPage);
                        break;
                    case OutputData.RegulatorType.INVERT:
                        try
                        {
                            ((MC34063A_Invert_Page)_paramsPage.SchematicPage).Click -= new MouseButtonEventHandler(SchematicClick);
                        }
                        catch (Exception) { }
                        ((MC34063A_Invert_Page)_paramsPage.SchematicPage).Click += new MouseButtonEventHandler(SchematicClick);
                        mainFrame.Navigate(_paramsPage.SchematicPage);
                        break;
                    case OutputData.RegulatorType.UP_DOWN:
                        try
                        {
                            ((MC34063A_StepUpDown_Page)_paramsPage.SchematicPage).Click -= new MouseButtonEventHandler(SchematicClick);
                        }
                        catch (Exception) { }
                        ((MC34063A_StepUpDown_Page)_paramsPage.SchematicPage).Click += new MouseButtonEventHandler(SchematicClick);
                        mainFrame.Navigate(_paramsPage.SchematicPage);
                        break;
                    default:
                        return;
                }
                this.Height = prevSize.Height;
                this.Width = prevSize.Width;
                MenuItemAnsichtBerechnung.IsChecked = false;
                if (MenuItemAnsichtSchaltung.IsChecked == false)
                    MenuItemAnsichtSchaltung.IsChecked = true;

                _viewerState = ViewerState.Schematic;
            }
        }

        private void SchematicClick(object sender, MouseButtonEventArgs e)
        {
            if (_viewerState == ViewerState.Schematic)
            {
                MenuItem_Ansicht_Berechnung_Click(sender, new RoutedEventArgs());
            }
        }


    }
}
